package cn.com.chec.en.util.images;

import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

public class ImageMarkLogoByIcon {
	public static void markImageByIcon(String iconPath, String srcImgPath,
			String tagertPath) {
		markImageByIcon(iconPath, srcImgPath, tagertPath, null);
	}

	public static void markImageByIcon(String iconPath, String srcImgPath,
			String tagertPath, Integer degree) {
		OutputStream os = null;
		try {
			Image srcImg = ImageIO.read(new File(srcImgPath));
			BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null),
					srcImg.getHeight(null), 1);

			Graphics2D g = buffImg.createGraphics();

			g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
					RenderingHints.VALUE_INTERPOLATION_BILINEAR);
			g.drawImage(
					srcImg.getScaledInstance(srcImg.getWidth(null),
							srcImg.getHeight(null), 4), 0, 0, null);
			if (degree != null) {
				g.rotate(Math.toRadians(degree.intValue()),
						buffImg.getWidth() / 2.0D, buffImg.getHeight() / 2.0D);
			}

			ImageIcon imgIcon = new ImageIcon(iconPath);
			Image img = imgIcon.getImage();

			float alpha = 0.5F;
			g.setComposite(AlphaComposite.getInstance(10, alpha));

			g.drawImage(img, srcImg.getWidth(null) / 2,
					srcImg.getHeight(null) - 100, null);
			g.setComposite(AlphaComposite.getInstance(3));
			g.dispose();
			os = new FileOutputStream(tagertPath);
			ImageIO.write(buffImg, "JPG", os);
		} catch (Exception e) {
			e.printStackTrace();
			try {
				if (os != null)
					os.close();
			} catch (Exception ex) {
				ex.printStackTrace();
			}
		} finally {
			try {
				if (os != null)
					os.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public static void main(String[] args) {
		String srcImgPath = "E:/ent/照片/王琪/温家宝.jpg";
		String iconPath = "E:/ent/照片/王琪/chec.jpg";
		String targerPath = "E:/ent/照片/王琪/温家宝_ico.jpg";

		markImageByIcon(iconPath, srcImgPath, targerPath);
	}
}